How to Remove Spaces from String in Python

您所在的位置:网站首页 sremove python How to Remove Spaces from String in Python

How to Remove Spaces from String in Python

2024-05-18 16:15:40| 来源: 网络整理| 查看: 265

Replacing spaces with replace() method in Python

To remove spaces from a string in Python, use the str.replace() method.

This method takes two arguments:

The first argument is the string to be replaced. The second argument is the string that replaces the string.

For example:

string = "This is a test" nospaces = string.replace(" ", "") print(nospaces)

Output:

Thisisatest

This is the quick answer.

In this guide, we take a look at other common situations related to removing spaces from a string.

How to Remove White Spaces in Python String

In Python, a string is an immutable type. This means it cannot be directly modified. This means any method that manipulates strings actually creates a new string.

In Python, there are many ways you can replace blank spaces:

str.strip() str.replace() str.join() with str.split() str.translate() re.sub()

Let’s go through each of these options and when you should use them.

1. str.strip()鈥擱emove Leading and Trailing Spaces

The str.strip() method removes the leading and trailing whitespace from a string.

For instance:

string = " This is a test " modified = string.strip() print(modified)

Output:

This is a test 2. str.replace()鈥擱emove All White Spaces

To wipe out all the white spaces from a string, you can use the str.replace() method.

This method takes two mandatory arguments:

The target strings you want to get rid of. The string that replaces all the target strings.

In other words, to remove the white spaces, replace each white space with an empty string.

For instance:

string = "This is a test" modified = string.replace(" ", "") print(modified)

Output:

Thisisatest 3. str.join() and str.split()鈥擱emove Duplicate White Spaces

To get rid of tabs, newlines, or any other duplicate whitespaces use str.join() method with str.split().

This works such that:

The str.split() method splits the string into a list of words without spaces. The str.join() takes a list of words and joins them using the str as a separator.

For example, let’s remove the duplicate white spaces but leave single white spaces:

before = "This \t is a \n test \n\n \t let's modify this" after = " ".join(before.split()) print(before) print(after)

Output:

This is a test let's modify this This is a test let's modify this 4. str.translate()鈥擱emove Tabs, Newlines, and Other White Spaces

To get rid of all the white spaces, tabs, and newlines, you can use the str.translate() method with str.maketrans() method.

The str.translate() method replaces strings according to a translation table created by str.maketrans() method. In short, the str.maketrans() method works such that it takes three arguments:

A string to be replaced. A string that specifies the characters to be replaced in the first argument. A list of characters to be removed from the original string. To remove white spaces, use string.whitespaces, which is a list of the types of blank spaces.

For instance:

import string before = "This \t is a \n test \n\n \t let's modify this" after = before.translate(str.maketrans("", "", string.whitespace)) print(before) print(after)

Output:

Thisisatestlet'smodifythis 5. re.sub()鈥擱eplace White Spaces with Empty Strings

RegEx or Regular Expression is like a Ctrl+F on steroids.

You can use it to match patterns in a string. You can for example use regex to find phone numbers or email addresses from a text document.

I am not going to go into more detail about regex, but here is a great article you can check.

To use Regular Expressions in Python to remove white spaces from a string:

Import the regex module, re, to your project. Define a regex pattern that matches white spaces. Substitute each white space with a blank space using re.sub() method.

For example:

import re string = "This is a test" # Regular expression that matches each white space whitespace = r"\s+" # Replace all mathces with an empty string nospaces = re.sub(whitespace, "", string) print(nospaces)

Output:

Thisisatest


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭